#include "Animation.h"

namespace lib3dw
{
	Animation::Animation(DrawableObject* animatedObject, Vector3 startPosition, Vector3 endPosition, float animationDuration)
	{
		object = animatedObject;
		start = startPosition;
		end = endPosition;
		duration = animationDuration;
		timeSpent = 0;
		finished = false;
	}
	
	Animation::~Animation()
	{
		delete object;
		delete start;
		delete end;
	}

	const bool Animation::IsFinished() const
	{
		return finished;
	}

	void Animation::Update(double frameTime)
	{	
		if(timeSpent <= duration)
		{
			const Vector3* currentPosition = object->GetModel()->GetTranslation();
			float nextX = currentPosition->x + (end.x - start.x) * (frameTime / duration);
			float nextY = currentPosition->y + (end.y - start.y) * (frameTime / duration);
			float nextZ = currentPosition->z + (end.z - start.z) * (frameTime / duration);
			object->GetModel()->SetTranslation(nextX, nextY, nextZ);
			timeSpent += frameTime;
		}
		else
		{
			finished = true;
			object->GetModel()->SetTranslation(end.x, end.y, end.z);
		}
	}
}